package com.teame.readbaby;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.teame.readbaby.adapter.AdapterForLinearLayout;
import com.teame.readbaby.parser.OrderDetailParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.view.LinearLayoutForListView;
import com.teame.readbaby.vo.Address;
import com.teame.readbaby.vo.Invoice;
import com.teame.readbaby.vo.OrderDetail;
import com.teame.readbaby.vo.Produt;

public class MyOrderDetailActivity extends BaseActivity {
	private TextView head_back_text;

	private TextView tv_orderdetail_consignee;
	private TextView tv_orderdetail_phonenumber;
	private TextView tv_orderdetail_address;
	private TextView tv_orderdetail_postal;

	private RelativeLayout ordr_logistics_rel;

	private TextView tv_orderdetail_status;
	private TextView tv_orderdetail_delivermodus;
	private TextView tv_orderdetail_payment;
	private TextView tv_orderdetail_investtime;
	private TextView tv_orderdetail_deliveryinfo;
	private TextView tv_orderdetail_isinvoice;
	private TextView tv_orderdetail_invoicetitle;
	private TextView tv_orderdetail_invoicecontent;
	private TextView tv_orderdetail_remark;

	private ListView listdetail;

	private TextView total_count;
	private TextView total_price;
	private TextView prom_cut;
	private TextView freight;
	private TextView total_point;
	
	private List<Produt> productlist;

	private String orderId;

	private RelativeLayout productcolor_isshow;

	private RelativeLayout productsize_isshow;
	
	private LinearLayoutForListView mListView = null;
	
	private String id;

	public void onClick1(View v) {
		Intent intent = null;
		int event = v.getId();
		switch(event){
		case R.id.head_back_text:
			intent = new Intent(context, MyOrderActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(intent);
			break;
		case R.id.ordr_logistics_rel:
			intent = new Intent(context, LogisticsActivity.class);
			intent.putExtra("orderId", orderId);
			startActivity(intent);
			break;
		}
	}

	@Override
	protected void findViewById() {
		head_back_text = (TextView) findViewById(R.id.head_back_text);// 返回按钮

		tv_orderdetail_consignee = (TextView) findViewById(R.id.tv_orderdetail_consignee);// 收件人
		tv_orderdetail_phonenumber = (TextView) findViewById(R.id.tv_orderdetail_phonenumber);// 收件人电话
		tv_orderdetail_address = (TextView) findViewById(R.id.tv_orderdetail_address);// 送货地址
		tv_orderdetail_postal = (TextView) findViewById(R.id.tv_orderdetail_postal);// 邮编

		tv_orderdetail_status = (TextView) findViewById(R.id.tv_orderdetail_status);// 订单状态
		tv_orderdetail_delivermodus = (TextView) findViewById(R.id.tv_orderdetail_delivermodus);// 送货方式
		tv_orderdetail_payment = (TextView) findViewById(R.id.tv_orderdetail_payment);// 支付方式
		tv_orderdetail_investtime = (TextView) findViewById(R.id.tv_orderdetail_investtime);// 下单时间
		tv_orderdetail_deliveryinfo = (TextView) findViewById(R.id.tv_orderdetail_deliverytime);// 送货时间
		tv_orderdetail_isinvoice = (TextView) findViewById(R.id.tv_orderdetail_isinvoice);// 是否开发票
		tv_orderdetail_invoicetitle = (TextView) findViewById(R.id.tv_orderdetail_invoicetitle);// 发票头
		tv_orderdetail_invoicecontent = (TextView) findViewById(R.id.tv_orderdetail_invoicecontent);// 发票内容
		tv_orderdetail_remark = (TextView) findViewById(R.id.tv_orderdetail_remark);// 备注

		total_count = (TextView) findViewById(R.id.total_count);// 商品金额总计 -
		total_price = (TextView) findViewById(R.id.total_price);// 商品金额总计 -
		prom_cut = (TextView) findViewById(R.id.prom_cut);// 促销减钱 ==> 优惠金额 -
		freight = (TextView) findViewById(R.id.freight);// 运费 -
		total_point = (TextView) findViewById(R.id.total_point);// 积分总计 -
		
		productcolor_isshow = (RelativeLayout) findViewById(R.id.lv_orderdetail_productcolor_isshow);
		productsize_isshow = (RelativeLayout) findViewById(R.id.lv_orderdetail_productsize_isshow);
		ordr_logistics_rel = (RelativeLayout) findViewById(R.id.ordr_logistics_rel);// 查询物流信息(按钮)
		
//		listdetail = (ListView) findViewById(R.id.listdetail);// 商品列表
		mListView = (LinearLayoutForListView) findViewById(R.id.listdetail);
		
		Intent intent = new Intent();
		orderId = intent.getStringExtra("orderId");
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.my_order_detail_activity);
	}

	@Override
	protected void processLogic() {
		String url = ConstantValue.HOST + ConstantValue.ORDERDETAIL;
		Map<String, String> params = new HashMap<String, String>();
		//TODO orderId从订单列表中获取
		params.put("orderId", orderId);
		getDataFromServer(url, params, new OrderDetailParser(), new DataCallback<OrderDetail>() {

			@Override
			public void processData(OrderDetail paramObject,
					boolean paramBoolean) {
				if(!paramBoolean || paramObject == null){
					//TODO ...
				}else{
					List<Object> checkout_prom = paramObject.getCheckout_prom();//促销信息

					/*
						tv_orderdetail_consignee = (TextView) findViewById(R.id.tv_orderdetail_consignee);// 收件人
						tv_orderdetail_phonenumber = (TextView) findViewById(R.id.tv_orderdetail_phonenumber);// 收件人电话
						tv_orderdetail_address = (TextView) findViewById(R.id.tv_orderdetail_address);// 送货地址
						tv_orderdetail_postal = (TextView) findViewById(R.id.tv_orderdetail_postal);// 邮编
					*/
					//TODO 获取电话号码和邮编号码没写??? 服务器没有返回相应数据, 也没有与用户表关联的键
					Address address = paramObject.getAddress();
					tv_orderdetail_consignee.setText(address.getName());
					tv_orderdetail_address.setText(address.getAddress_area()+address.getAddress_detail());
					
					/*
						tv_orderdetail_status = (TextView) findViewById(R.id.tv_orderdetail_status);// 订单状态
						tv_orderdetail_delivermodus = (TextView) findViewById(R.id.tv_orderdetail_delivermodus);// 送货方式
						tv_orderdetail_payment = (TextView) findViewById(R.id.tv_orderdetail_payment);// 支付方式
						tv_orderdetail_investtime = (TextView) findViewById(R.id.tv_orderdetail_investtime);// 下单时间
						tv_orderdetail_deliveryinfo = (TextView) findViewById(R.id.tv_orderdetail_deliverytime);// 送货时间
						tv_orderdetail_isinvoice = (TextView) findViewById(R.id.tv_orderdetail_isinvoice);// 是否开发票
						tv_orderdetail_invoicetitle = (TextView) findViewById(R.id.tv_orderdetail_invoicetitle);// 发票头
						tv_orderdetail_invoicecontent = (TextView) findViewById(R.id.tv_orderdetail_invoicecontent);// 发票内容
						tv_orderdetail_remark = (TextView) findViewById(R.id.tv_orderdetail_remark);// 备注
					 */
					tv_orderdetail_status.setText(paramObject.getStatus());
					
					tv_orderdetail_delivermodus.setText("红孩子送货");
					
					int type = paramObject.getPayment_info();
					String delivery = "";
					switch(type){
					//　　1=>货到付款 2=>货到POS机  3=>支付宝(待定)
					case 1:
						delivery = "货到付款";
						break;
					case 2:
						delivery = "货到POS机";
						break;
					case 3:
						delivery = "支付宝";
						break;
					}
					tv_orderdetail_payment.setText(delivery);
					
					tv_orderdetail_investtime.setText(paramObject.getTime());
					
					int type1 = paramObject.getDelivery_info();
					String delivery1 = "";
					switch(type1){
					//1 => 周一至周五送货 2=> 双休日及公众假期送货 3=> 时间不限，工作日双休日及公众假期均可送货
					case 1:
						delivery1 = "周一至周五送货";
						break;
					case 2:
						delivery1 = "双休日及公众假期送货";
						break;
					case 3:
						delivery1 = "时间不限，工作日双休日及公众假期均可送货";
						break;
					}
					tv_orderdetail_deliveryinfo.setText(delivery1);
					
					Invoice invoice = paramObject.getInvoice();
					delivery = invoice != null ? "有发票" : "没有发票";
					tv_orderdetail_isinvoice.setText(delivery);
					
					tv_orderdetail_invoicetitle.setText(invoice.getTitle());
					
					tv_orderdetail_invoicecontent.setText(invoice.getContent());
					
					//TODO 备注信息服务器没有传过来, 应该从哪里获取???
//					tv_orderdetail_remark.setText("");
					
					productlist = paramObject.getProductlist();
					List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
					if(productlist != null){
						for (Produt produt : productlist) {
							HashMap<String, Object> map = new HashMap<String, Object>();
							id = produt.getId();
							map.put("id", id);
							map.put("name", produt.getName());
							map.put("number", "共"+ produt.getNumber()+ "件商品");
							map.put("price", "￥："+ produt.getPrice());
							String color = produt.getColor();
							if(color == null){
								productcolor_isshow.setVisibility(View.GONE);
								map.put("color", "");
							}else{
								map.put("color", color);
							}
							String size = produt.getSize();
							if(size == null){
								productsize_isshow.setVisibility(View.GONE);
								map.put("size", "");
							}else{
								map.put("size", size);
							}
							//处理isgift==>是否赠品    和    pic==>图片
							String isgift = produt.getIsgift();
							map.put("isgift", ("yes".equals(isgift) ? "有赠品" : "无赠品"));
							
							data.add(map);
						}
						String[] from = new String[]{"id", "name", "number", "price", "color", "size"};
						int[] to = new int[]{R.id.lv_orderdetail_productid, R.id.lv_orderdetail_productname, 
								R.id.lv_orderdetail_productnumber, R.id.lv_orderdetail_productprice, 
								R.id.lv_orderdetail_productcolor, R.id.lv_orderdetail_productsize};
//						listdetail.setAdapter(new SimpleAdapter(context, data, R.layout.my_order_detail_listitem, from, to));
						
						AdapterForLinearLayout adapter = new AdapterForLinearLayout(context, data, R.layout.my_order_detail_listitem, from, to);
//						findViewById(R.id.list_hotcity);
						mListView.setAdapter(adapter);
					}
					
					
					/*
						total_count = (TextView) findViewById(R.id.total_count);// 商品金额总计 -
						total_price = (TextView) findViewById(R.id.total_price);// 商品金额总计 -
						prom_cut = (TextView) findViewById(R.id.prom_cut);// 促销减钱 ==> 优惠金额 -
						freight = (TextView) findViewById(R.id.freight);// 运费 -
						total_point = (TextView) findViewById(R.id.total_point);// 积分总计 -
					 */
					
					total_count.setText(paramObject.getTotal_count()+"件商品");
					total_price.setText("￥："+ paramObject.getTotal_price());
					prom_cut.setText("￥："+paramObject.getProm_cut());
					freight.setText("￥："+paramObject.getFreight());
					total_point.setText(paramObject.getTotal_point()+"积分");
				}
			}
		});
	}

	@Override
	protected void setListener() {
/*		listdetail.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//TODO 开启商品详情界面
//				Intent intent = new Intent(context, XXXX.class);
//				intent.putExtra("id", id);
//				startActivity(intent);
			}
			
		});*/
		
		mListView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//TODO 开启商品详情界面
//				Intent intent = new Intent(context, XXXX.class);
//				intent.putExtra("id", id);
//				startActivity(intent);
			}
		});//点击事件
		
		head_back_text.setOnClickListener(this);
		ordr_logistics_rel.setOnClickListener(this);

	}

	@Override
	protected void onClickEvent(View paramView) {
		onClick1(paramView);
	}

}
